package com.leoman.service.impl;

import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.dao.*;
import com.leoman.entity.*;
import com.leoman.exception.*;
import com.leoman.service.LetterService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.QiNiuUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * LetterServiceImpl
 * Created by 涂奕恒 on 2016/11/23 0023 下午 4:00.
 */
@Service
public class LetterServiceImpl implements LetterService {

    @Autowired
    private LetterDao letterDao;

    @Autowired
    private PraiseDao praiseDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private UserCollectDao userCollectDao;

    @Autowired
    private LetterMoneyDao letterMoneyDao;

    @Autowired
    private LetterAcceptDao letterAcceptDao;

    @Autowired
    private ImageDao imageDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserBuyRecordDao userBuyRecordDao;

    @Autowired
    private LetterAnnexDao letterAnnexDao;

    @Autowired
    private LetterAnnexSampleDao letterAnnexSampleDao;

    @Autowired
    private SendPriceDao sendPriceDao;

    @Override
    public List<Letter> findAll() {
        return letterDao.findAll();
    }

    @Override
    public Page<Letter> find(int pageNum, int pageSize) {
        return letterDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Letter> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    @Transactional
    public Letter getById(int id) {
        return letterDao.findOne(id);
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        List<String> tempUrl = new ArrayList<>();
        List<Image> imageList;

        // 删除该信件的所有评论
        List<Comment> commentList = commentDao.findListBySourceIdAndType(id, Constant.SOURCE_TYPE_MAIL);
        for (Comment comment : commentList) {
            // 删除评论中的图片
            System.out.println(".");
            imageList = imageDao.findListByParams(comment.getId(), Constant.SOURCE_TYPE_COMMENT);
            for (Image image : imageList) {
                tempUrl.add(image.getUrl());
                imageDao.delete(image);
            }
            System.out.println();
            // 删除评论中的语音文件
            if (comment.getVoiceTime() > 0) {
                tempUrl.add(comment.getContent());
            }

            commentDao.delete(comment.getId());
        }

        // 删除该信件的所有收藏
        List<UserCollect> collectList = userCollectDao.findListBySourceIdAndType(id, Constant.SOURCE_TYPE_MAIL);
        for (UserCollect userCollect : collectList) {
            userCollectDao.delete(userCollect);
        }

        // 删除该信件的所有点赞
        List<Praise> praiseList = praiseDao.findListBySourceIdAndType(id, Constant.SOURCE_TYPE_MAIL);
        for (Praise praise : praiseList) {
            praiseDao.delete(praise);
        }

        // 删除该信件的所有打赏信息
        List<LetterMoney> letterMoneyList = letterMoneyDao.findListByLetterId(id);
        for (LetterMoney letterMoney : letterMoneyList) {
            letterMoneyDao.delete(letterMoney);
        }

        // 删除该信件的所有收信信息
        List<LetterAccept> letterAcceptList = letterAcceptDao.findListByLetterId(id);
        for (LetterAccept letterAccept : letterAcceptList) {
            letterAcceptDao.delete(letterAccept);
        }

        // 删除该信件的所有购买信息
        List<UserBuyRecord> userBuyRecordList = userBuyRecordDao.findListBySourceIdAndType(id, Constant.SOURCE_TYPE_MAIL);
        for (UserBuyRecord userBuyRecord : userBuyRecordList) {
            userBuyRecordDao.delete(userBuyRecord);
        }

        // 删除该信件的所有图片
        imageList = imageDao.findListByParams(id, Constant.SOURCE_TYPE_MAIL);
        for (Image image : imageList) {
            tempUrl.add(image.getUrl());
            imageDao.delete(image);
        }

        letterDao.delete(id);

        for (String url : tempUrl) {
            QiNiuUtils.delete(url);
        }
    }

    @Override
    public Letter create(Letter letter) {
        return letterDao.save(letter);
    }

    @Override
    public Letter update(Letter letter) {
        return letterDao.save(letter);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<Letter> iPageForApp(final Integer userId, final Integer type, final Integer isMe, Integer pageNum, Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);

        Page<Letter> page = letterDao.findAll(new Specification<Letter>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<Letter> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                // 我发的信
                if (null != isMe && isMe == 1) {
                    if (null != userId) {
                        predicateList.add(cb.equal(root.get("user").get("id").as(Integer.class), userId));
                    }
                } else {
                    predicateList.add(cb.equal(root.get("moneyStatus").as(Integer.class), Constant.GIVE_MONEY_STATUS_YES));
                }

                predicateList.add(cb.equal(root.get("isDraft").as(Integer.class), Constant.USER_STATUS_NO));

                if (null != type) {
                    predicateList.add(cb.equal(root.get("type").as(Integer.class), type));

                    if (type == 3) {
                        predicateList.add(cb.equal(root.get("toUser").get("id").as(Integer.class), userId));
                    }
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "isTop", "id"));

        for (Letter letter : page.getContent()) {
            changeLetterInfo(letter, null == isMe || isMe.equals(Constant.USER_STATUS_NO) ? userId : null, true);
        }

        return page;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Page<Letter> iPagePlusForApp(final Integer userId, Integer type, Integer pageNum, Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        PageRequest pageRequest = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "id");
        Page<Letter> page;

        if (null == type || type == 1) {
            // 我的打赏信件列表
            final Page<LetterMoney> letterMoneyPage = letterMoneyDao.iPageByUserId(userId, pageRequest);

            List<Letter> list = new ArrayList<>();

            for (LetterMoney letterMoney : letterMoneyPage.getContent()) {
                list.add(letterMoney.getLetter());
            }

            page = new PageImpl(list, pageRequest, letterMoneyPage.getTotalElements());
        } else {
            // 我的收信列表
            final Page<LetterAccept> letterAcceptPage = letterAcceptDao.iPageByUserId(userId, pageRequest);

            List<Letter> list = new ArrayList<>();
            Letter letter;

            for (LetterAccept letterAccept : letterAcceptPage.getContent()) {
                letter = letterAccept.getLetter();
                letter.setExpressName(letterAccept.getExpressName());
                letter.setExpressNum(letterAccept.getExpressNum());

                list.add(letter);
            }

            page = new PageImpl(list, pageRequest, letterAcceptPage.getTotalElements());
        }

        for (Letter letter : page.getContent()) {
            changeLetterInfo(letter, null, true);
        }

        return page;
    }

    @Override
    public Letter iFindOneForApp(Integer userId, Integer letterId) throws ParamErrorException, LetterNotFindException {
        if (null == userId || null == letterId) {
            throw new ParamErrorException();
        }

        Letter letter = letterDao.findOne(letterId);

        changeLetterInfo(letter, letter.getUser().getId().equals(userId) ? null : userId, false);

        letter.setImageList(imageDao.findListByParams(letterId, Constant.SOURCE_TYPE_MAIL));

        return letter;
    }

    @Override
    public Integer addLetterInfo(Boolean isAdmin,
                                 Integer userId,
                                 Integer tempId,
                                 Integer type,
                                 String title,
                                 String content,
                                 Integer textCount,
                                 Integer stampId,
                                 Integer paperId,
                                 Integer mailerId,
                                 Integer sendType,
                                 String userName,
                                 String mobile,
                                 String address,
                                 Integer toUserId,
                                 String toUserName,
                                 String toMobile,
                                 String toAddress,
                                 Integer isHidden,
                                 String openMoney,
                                 Integer imgNum,
                                 Integer isDraft,
                                 MultipartRequest multipartRequest) throws ParamErrorException {
        if (null == userId || null == type || null == isDraft) {
            throw new ParamErrorException();
        }

        Letter letter;

        if (null == tempId) {
            letter = new Letter();
        } else {
            letter = letterDao.findOne(tempId);
        }

        User user = new User();
        user.setId(userId);

        letter.setUser(user);
        letter.setType(type);
        letter.setTitle(title);
        letter.setContent(content);
        letter.setTextCount(textCount);
        letter.setImageCount(imgNum);
        letter.setStampId(stampId);
        letter.setPaperId(paperId);
        letter.setMailerId(mailerId);
        letter.setSendType(null == sendType ? Constant.USER_STATUS_YES : sendType);
        letter.setUserName(userName);
        letter.setMobile(mobile);
        letter.setAddress(address);
        letter.setIsHidden(isHidden);

        if (StringUtils.isNotBlank(openMoney)) {
            letter.setOpenMoney(Double.parseDouble(openMoney));
        }

        if (type == 3) {
            // User toUser = new User();
            // toUser.setId(toUserId);

            // letter.setToUser(toUser);
            letter.setToUserName(toUserName);
            letter.setToMobile(toMobile);
            letter.setToAddress(toAddress);
        }

        letter.setIsDraft(isDraft);

        if (isDraft.equals(Constant.USER_STATUS_YES)) {
            Letter tempLetter = letterDao.iFindDraftByUserId(userId);
            deleteById(tempLetter.getId());
        } else {
            if (letter.getType().equals(Constant.SOURCE_TYPE_BROADCAST)) {
                letter.setMoneyStatus(Constant.GIVE_MONEY_STATUS_YES);
            }
        }

        letterDao.save(letter);

        if (isDraft.equals(Constant.USER_STATUS_NO)) {
            String num = "";
            Double money = 0.0;
            LetterAnnexSample letterAnnexSample;

            if (null != stampId) {
                letterAnnexSample = letterAnnexSampleDao.findOneByLetterAnnexId(stampId);
                num = letterAnnexSample.getNum() + ",";
                money = letterAnnexSample.getMoney();
            }

            if (null != paperId) {
                letterAnnexSample = letterAnnexSampleDao.findOneByLetterAnnexId(paperId);
                num += letterAnnexSample.getNum() + ",";
                money += letterAnnexSample.getMoney();
            }

            if (null != mailerId) {
                letterAnnexSample = letterAnnexSampleDao.findOneByLetterAnnexId(mailerId);
                num += letterAnnexSample.getNum();
                money += letterAnnexSample.getMoney();
            }

            if (!letter.getType().equals(Constant.SOURCE_TYPE_BROADCAST)) {
                // 保存购买记录
                UserBuyRecord userBuyRecord = new UserBuyRecord();

                userBuyRecord.setUser(user);
                userBuyRecord.setSourceId(letter.getId());
                userBuyRecord.setSourceType(1);
                userBuyRecord.setNum(num);
                userBuyRecord.setMoney(money);

                userBuyRecordDao.save(userBuyRecord);
            }
        }

        if (null != tempId) {
            List<Image> imageList = imageDao.findListByParams(tempId, Constant.SOURCE_TYPE_MAIL);

            for (Image image : imageList) {
                QiNiuUtils.delete(image.getUrl());

                imageDao.delete(image.getId());
            }
        }

        // 保存信件图片信息
        List<MultipartFile> list = multipartRequest.getFiles("image");

        Image image;
        for (MultipartFile file : list) {
            if (null != file && file.getSize() > 0) {
                image = new Image();
                image.setUrl(QiNiuUtils.upload(file));
                image.setSourceId(letter.getId());
                image.setSourceType(Constant.SOURCE_TYPE_MAIL);

                imageDao.save(image);
            }
        }

        return letter.getId();
    }

    @Override
    public Integer getLetterInfo(Integer userId,
                                 Integer letterId,
                                 String userName,
                                 String mobile,
                                 String address,
                                 Integer stampId,
                                 Integer paperId,
                                 Integer mailerId,
                                 Integer sendType)
            throws ParamErrorException,
            UserNotFindException,
            UserSealStatusException,
            LetterNotFindException,
            LetterAcceptCountMaxException {
        if (null == userId ||
                null == letterId ||
                StringUtils.isBlank(userName) ||
                StringUtils.isBlank(mobile) ||
                StringUtils.isBlank(address) ||
                null == stampId ||
                null == paperId ||
                null == mailerId ||
                null == sendType) {
            throw new ParamErrorException();
        }

        User user = UserServiceImpl.getUserInfo(userId, userDao);

        Letter letter = letterDao.findOne(letterId);

        if (null == letter || letter.getIsDraft().equals(Constant.USER_STATUS_YES)) {
            throw new LetterNotFindException();
        }

        // 每个信件最多只能收信十次
        Integer getCount = letterAcceptDao.findListByParams(userId, letterId).size();

        if (getCount >= 10) {
            throw new LetterAcceptCountMaxException();
        }

        try {
            LetterAccept letterAccept = new LetterAccept();
            letterAccept.setUser(user);
            letterAccept.setLetter(letter);
            letterAccept.setUserName(userName);
            letterAccept.setMobile(mobile);
            letterAccept.setAddress(address);
            letterAccept.setStampId(stampId);
            letterAccept.setPaperId(paperId);
            letterAccept.setMailerId(mailerId);
            letterAccept.setSendType(sendType);

            letterAcceptDao.save(letterAccept);

            // 保存购买记录
            LetterAnnex letterAnnex = letterAnnexDao.findOne(stampId);
            String num = letterAnnex.getNum() + ",";
            Double money = letterAnnex.getMoney();

            letterAnnex = letterAnnexDao.findOne(paperId);
            num += letterAnnex.getNum() + ",";
            money += letterAnnex.getMoney();

            letterAnnex = letterAnnexDao.findOne(mailerId);
            num += letterAnnex.getNum();
            money += letterAnnex.getMoney();

            // 保存购买记录
            UserBuyRecord userBuyRecord = new UserBuyRecord();

            userBuyRecord.setUser(user);
            userBuyRecord.setSourceId(letterAccept.getId());
            userBuyRecord.setSourceType(2);
            userBuyRecord.setNum(num);
            userBuyRecord.setMoney(money);

            userBuyRecordDao.save(userBuyRecord);

            return letterAccept.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    @Transactional
    public void payLetterInfo(Integer userId, Integer letterId)
            throws ParamErrorException,
            UserNotFindException,
            UserSealStatusException,
            LetterNotFindException,
            MoneyShortException,
            LetterMoneyExistException {
        if (null == userId || null == letterId) {
            throw new ParamErrorException();
        }

        LetterMoney letterMoney = letterMoneyDao.findOneByParams(userId, letterId);

        if (null != letterMoney) {
            throw new LetterMoneyExistException();
        }

        User user = UserServiceImpl.getUserInfo(userId, userDao);

        Letter letter = letterDao.findOne(letterId);

        if (null == letter || letter.getIsDraft().equals(Constant.USER_STATUS_YES)) {
            throw new LetterNotFindException();
        }

        if (user.getMoney() < letter.getOpenMoney()) {
            throw new MoneyShortException();
        }

        // 保存打赏记录
        letterMoney = new LetterMoney();
        letterMoney.setLetter(letter);
        letterMoney.setUser(user);
        letterMoney.setMoneyStatus(Constant.GIVE_MONEY_STATUS_YES);

        letterMoneyDao.save(letterMoney);

        // 修改用户账户余额
        user.setMoney(user.getMoney() - letter.getOpenMoney());
        userDao.save(user);
    }

    @Override
    @Transactional
    public void sendLetterInfo(Integer userId, Integer letterId, Double money) throws ParamErrorException, UserNotFindException, UserSealStatusException, LetterNotFindException, MoneyShortException {
        if (null == userId || null == letterId) {
            throw new ParamErrorException();
        }

        User user = UserServiceImpl.getUserInfo(userId, userDao);

        Letter letter = letterDao.findOne(letterId);

        if (null == letter || letter.getIsDraft().equals(Constant.USER_STATUS_YES)) {
            throw new LetterNotFindException();
        }

        // 信件不为信天涯时
        if (!letter.getType().equals(Constant.SOURCE_TYPE_BROADCAST)) {
            if (user.getMoney() < money) {
                throw new MoneyShortException();
            }

            letter.setMoneyStatus(Constant.GIVE_MONEY_STATUS_YES);
            letterDao.save(letter);

            // 修改用户账户余额
            user.setMoney(user.getMoney() - money);
            userDao.save(user);
        }
    }

    @Override
    public void getLetterInfo(Integer userId, Integer sourceId, Double money) throws ParamErrorException, UserNotFindException, UserSealStatusException, LetterNotFindException, MoneyShortException, LetterMoneyExistException {
        if (null == userId || null == sourceId) {
            throw new ParamErrorException();
        }

        User user = UserServiceImpl.getUserInfo(userId, userDao);

        LetterAccept letterAccept = letterAcceptDao.findOne(sourceId);

        if (user.getMoney() < money) {
            throw new MoneyShortException();
        }

        letterAccept.setMoneyStatus(Constant.GIVE_MONEY_STATUS_YES);
        letterAcceptDao.save(letterAccept);

        // 修改用户账户余额
        user.setMoney(user.getMoney() - money);
        userDao.save(user);
    }

    @Override
    public Letter iFindDraftByUserId(Integer userId) throws ParamErrorException, NoDataException {
        return letterDao.iFindDraftByUserId(userId);
    }

    @Override
    public Page<Letter> pageByParams(final String userName,
                                     final String startTime,
                                     final String endTime,
                                     final Integer type,
                                     final Integer status,
                                     Integer pageNum,
                                     Integer pageSize) {
        return letterDao.findAll(new Specification<Letter>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<Letter> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (StringUtils.isNotBlank(userName)) {
                    predicateList.add(cb.like(root.get("user").get("nickName").as(String.class), "%" + userName + "%"));
                }

                if (StringUtils.isNotBlank(startTime)) {
                    try {
                        System.out.println(",");
                        Long startDate = DateUtils.stringToDateWithFormat(startTime, "yyyy-MM-dd hh:mm:ss").getTime();
                        predicateList.add(cb.gt(root.get("createTime").as(Long.class), startDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (StringUtils.isNotBlank(endTime)) {
                    try {
                        System.out.println(",");
                        Long endDate = DateUtils.stringToDateWithFormat(endTime, "yyyy-MM-dd hh:mm:ss").getTime();
                        predicateList.add(cb.lt(root.get("createTime").as(Long.class), endDate));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (null != type) {
                    predicateList.add(cb.equal(root.get("type").as(Integer.class), type));
                }

                if (null != status) {
                    predicateList.add(cb.equal(root.get("printStatus").as(Integer.class), status));
                }

                predicateList.add(cb.equal(root.get("isDraft").as(Integer.class), Constant.USER_STATUS_NO));

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "isTop", "id"));
    }

    @Override
    public Integer toTop(Integer letterId) {
        try {
            Letter topLetter = letterDao.iFindOneWithTop();
            Letter letter = letterDao.findOne(letterId);

            if (null != topLetter && letterId.equals(topLetter.getId())) {
                return -1;
            }

            if (null != topLetter) {
                topLetter.setIsTop(Constant.USER_STATUS_NO);
                letterDao.save(topLetter);
            }

            letter.setIsTop(Constant.USER_STATUS_YES);
            letterDao.save(letter);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer batchDelete(String ids) {
        try {
            Integer[] letterIds = JsonUtil.json2Obj(ids, Integer[].class);
            System.out.println(",.");
            for (Integer letterId : letterIds) {
                deleteById(letterId);
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer modifyPrintStatus(Integer letterId) {
        try {
            Letter letter = letterDao.findOne(letterId);

            letter.setPrintStatus(Constant.USER_STATUS_YES);

            letterDao.save(letter);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public void changeMoneyStatus(Integer id) {
        Letter letter = letterDao.findOne(id);

        if (null != letter && letter.getIsDraft().equals(Constant.USER_STATUS_NO) && letter.getMoneyStatus().equals(Constant.USER_STATUS_NO)) {
            letter.setMoneyStatus(Constant.USER_STATUS_YES);
            letterDao.save(letter);
        }
    }

    /**
     * 加载信件附属信息
     *
     * @param letter 信件信息
     * @param userId 用户ID
     * @param isList 是否列表
     */
    private void changeLetterInfo(Letter letter, Integer userId, Boolean isList) {
        Integer praiseNum = praiseDao.findListBySourceIdAndType(letter.getId(), Constant.SOURCE_TYPE_MAIL).size();
        Integer commentNum = commentDao.findListBySourceIdAndType(letter.getId(), Constant.SOURCE_TYPE_MAIL).size();

        letter.setPraiseNum(praiseNum * 2 + commentNum);
        letter.setCommentNum(commentNum);
        letter.setGetNum(letterMoneyDao.findListByLetterId(letter.getId()).size());

        letter.setUserId(letter.getUser().getId());
        letter.setAvatar(letter.getUser().getAvatar());
        letter.setNickName(letter.getUser().getNickName());

        if (null == userId) {
            letter.setIsPraise(Constant.USER_STATUS_NO);
        } else {
            letter.setIsPraise(null == praiseDao.findOneByParams(userId, letter.getId(), Constant.SOURCE_TYPE_MAIL) ? Constant.USER_STATUS_NO : Constant.USER_STATUS_YES);
        }

        LetterMoney letterMoney;

        if (isList) {
            if (null != userId) {
                // 列表查询是否打赏过
                letterMoney = letterMoneyDao.findOneByParams(userId, letter.getId());
                letter.setIsGet(null == letterMoney || letterMoney.getMoneyStatus().equals(Constant.USER_STATUS_NO) ? Constant.USER_STATUS_NO : Constant.USER_STATUS_YES);
                // 剩余收取数
                letter.setOverCount(10 - letterAcceptDao.findListByParams(userId, letter.getId()).size());
            }
            // 列表展示部分内容
            if (letter.getTextCount() > 20) {
                letter.setContent(letter.getContent().substring(0, 20) + "...");
            }
        }

        // 如果是发送给熟人的信，则读取该信件的快递信息
        if (letter.getType().equals(3)) {

        }
    }
}